<?php
class PreparationREQArticle
{
    //Variables qui contiendront la  pr�paration des requ�tes
    private $ReqSelectById;
    private $ReqUpdateContenuById;
    private $ReqUpdateTitreById;
    private $ReqUpdateIdRubriqueById;
    private $ReqUpdateDateDebutValideById;
    private $ReqUpdateDateFinValideById;
    private $ReqSelectAll;
    private $ReqTrouverID;
    private $ReqTrouverDernierID;
    private $ReqInsert;
    private $ReqTruncate;
    private $ReqSelectAllRubrique;
    private $ReqUpdateAllById;
    private $ReqSelectAllAlbum;
    private $ReqSuppPublier;
    private $ReqSuppContenir;
    private $ReqSuppArticle;


    public function __construct($db)
    {
    	//variables renseign�es lors de la pr�paration des requ�tes
	
        $this->ReqSelectAll = $db->prepare("SELECT * FROM Article as A INNER JOIN rubrique as R ON A.ART_ID_DISPOSER=R.RUB_ID INNER JOIN publier as P ON A.ART_ID=P.ID_1 INNER JOIN administrateur as AD ON P.ID=AD.ADMIN_ID INNER JOIN contenir AS C ON C.ID = A.ART_ID INNER JOIN Image as I ON I.IM_ID=C.ID_1 ORDER BY A.ART_DATEDEBVALID DESC");
		
	    $this->ReqSelectById = $db->prepare("SELECT A.ART_ID, A.ART_ID_DISPOSER, A.ART_TITRE, A.ART_CONTENU, A.ART_DATEDEBVALID, A.ART_DATEFINVALID, A.ART_ADM_MODIFIER, R.RUB_ID, R.RUB_LIBELLE, AD.ADMIN_ID, AD.ADMIN_NOM, AD.ADMIN_PRENOM, I.IM_ID, I.IM_ID_APPARTENIR, I.IM_TITRE, I.IM_DESCRIPTION, I.IM_FICHIER, AL.ALB_ID, AL.ALB_Titre, AL.ALB_URL FROM Article as A INNER JOIN rubrique as R ON A.ART_ID_DISPOSER=R.RUB_ID INNER JOIN publier as P ON A.ART_ID=P.ID_1 INNER JOIN administrateur as AD ON P.ID=AD.ADMIN_ID INNER JOIN contenir AS C ON C.ID = A.ART_ID INNER JOIN Image as I ON I.IM_ID=C.ID_1 INNER JOIN Album AS AL ON I.IM_ID_APPARTENIR = AL.ALB_ID WHERE A.ART_ID = ?");
        
        $this->ReqUpdateContenuById = $db->prepare("UPDATE article SET ART_CONTENU = ? WHERE ART_ID= ?");
        
        $this->ReqUpdateTitreById = $db->prepare("UPDATE article SET ART_TITRE = ? WHERE ART_ID = ?");
        
        $this->ReqUpdateAllById = $db->prepare("UPDATE article SET ART_TITRE = ?, ART_CONTENU = ?, ART_ID_DISPOSER = ?  WHERE ART_ID = ?");
        
        $this->ReqUpdateIdRubriqueById = $db->prepare("UPDATE article SET ART_ID_DISPOSER = ? WHERE ART_ID = ?");
       
        $this->ReqUpdateDateDebutValideById = $db->prepare("UPDATE article SET ART_DATEDEBVALID = ? WHERE ART_ID = ?");
        
        $this->ReqUpdateDateFinValideById = $db->prepare("UPDATE article SET ART_DATEFINVALID = ? WHERE ART_ID = ?");
        
        $this->ReqTrouverDernierID = $db->prepare("SELECT IMID FROM image WHERE IMID = LAST_INSERT_ID()");
        
 	    $this->ReqTrouverID = $db->prepare("SELECT IMID FROM image WHERE IMTITRE = ? AND IMFICHIER = ? IMID_APPARTENIR = ?");
 	    
		$this->ReqInsert = $db->prepare("INSERT INTO article(ART_ID_DISPOSER, ART_TITRE, ART_CONTENU, ART_DATEDEBVALID, ART_FINVALID) VALUES (:ARTID_DISPOSER, :ARTTITRE, :ARTCONTENU, :ARTDATEDEBVALID, :ARTFINVALID)");
		
       $this->ReqTruncate = $db->prepare("TRUNCATE article");

       $this->ReqSelectAllRubrique = $db->prepare("SELECT * FROM Rubrique");
       
       $this->ReqSelectAllAlbum = $db->prepare("SELECT * FROM album");
       
       $this->ReqSuppArticle = $db->prepare('DELETE FROM article WHERE ART_ID = ?');
       $this->ReqSuppContenir = $db->prepare('DELETE FROM contenir WHERE ID = ?');
       $this->ReqSuppPublier = $db->prepare ('DELETE FROM publier WHERE ID_1 = ?');
	   
    }

//M�thodes publiques qui ex�cuteront les requ�tes d�j� pr�par�es dans le constructeur.
	public function insert($vtitre, $vcontenu, $vdatedebutvalide, $vdatefinvalide)
    {
		$this->ReqInsert->execute(array(':ARTID_DISPOSER' => $vUneRubrique , ':ARTTITRE' => $vtitre, ':ARTCONTENU' => $vcontenu, ':ARTDATEDEBVALID' => $vdatedebutvalide, ':ARTFINVALID' => $vdatefinvalide));
    	
        return $this->ReqTrouverDernierID->fetch();
    }
    
   	Public function UpdateContenu ($Contenu, $ID)
    {
    	return $this->ReqUpdateContenuById->execute(array($Contenu, $ID));
    }
    
    Public function UpdateTitre ($Titre, $ID)
    {
    	return $this->ReqUpdateTitreById->execute(array($Titre, $ID));
    }
    
    Public function UpdateIdRubrique ($IDRubrique, $ID)
    {
    	return $this->ReqUpdateIdRubriqueById->execute(array($IDRubrique, $ID));
    }
    
    public function UpdateAllById($titre, $contenu, $rubrique, $ID)
    {
    	return $this->ReqUpdateAllById->execute(array($titre, $contenu, $rubrique, $ID));
    }
    
    Public function UpdateDateDebutValide ($DateDebutValide, $ID)
    {
    	return $this->ReqUpdateDateDebutValideById->execute(array($DateDebutValide, $ID));
    }
    
    Public function UpdateDateFinValide ($DateFinValide, $ID)
    {
    	return $this->ReqUpdateDateFinValideById->execute(array($DateFinValide, $ID));
    }
    
    public function TrouverID ($vtitre, $vfichier, $vUnAlbum)
    {
    	return $this->ReqTrouverID->fetch(array($vtitre, $vfichier, $vUnAlbum));
    }

    public function SelectAll()
    {
    	$this->ReqSelectAll->execute();
        return $this->ReqSelectAll->fetchAll();
    }
    
    public function SelectAllRubrique()
    {
    	$this->ReqSelectAllRubrique->execute();
    	return $this->ReqSelectAllRubrique->fetchAll();
    }

	public function selectById($id)
    {
        $this->ReqSelectById->execute(array($id));
        return $this->ReqSelectById ->fetch();
    }

    public function selectByName($name)
    {
        $this->ReqSelectByName->execute(array('%'.$name.'%'));
        return $this->ReqSelectByName->fetchAll();
    }
    
	public function truncate()
    {
        return $this->ReqTruncate->execute();
    }
    
    public function SelectAllAlbum ()
    {
    	$this->ReqSelectAllAlbum->execute();
    	return $this->ReqSelectAllAlbum->fetchall();
    }
    
    public function SupprimerArticle($vID)
    {
    	$vretour = "";
    	$vretour = $this->ReqSuppPublier->execute(array($vID));
    	$vretour = $vretour.$this->ReqSuppContenir->execute(array($vID));
    	sleep(1);
    	$vretour = $vretour.$this->ReqSuppArticle->execute(array($vID));
    	return $vretour;
    }
	
}